﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class TrainNeedsEdit : System.Web.UI.Page
    {
        protected TrainNeed trainNeed;
        protected string empName;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                string formNo = Request.QueryString["formNo"];
                if (!string.IsNullOrEmpty(formNo))
                {
                    trainNeed = ctx.TrainNeeds.Single(r => r.FormNo == formNo);
                    empName = ctx.Employees.Single(r => r.Id == trainNeed.EmpId).Name;
                    txtWorkItems.Value = trainNeed.WorkItems;
                    txtWorkTargets.Value = trainNeed.WorkTargets;
                    txtFocusIssue.Value = trainNeed.FocusIssue;
                    txtTrainItems.Value = trainNeed.TrainItems;
                    txtTrainBasic.Value = trainNeed.TrainBasic;
                    txtTrainExpert.Value = trainNeed.TrainExpert;
                    txtTrainManagement.Value = trainNeed.TrainManagement;
                    txtTrainAhead.Value = trainNeed.TrainAhead;
                    txtTrainSuggestion.Value = trainNeed.TrainSuggestion;
                }
                else
                {
                    EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
                    string deptName = ctx.Depts.Single(r => r.Id == ebi.EmpDepPos.OrderByDescending(r2 => r2.PosId).First().DeptId).Text;
                    empName = ebi.Name;
                    trainNeed = new TrainNeed();
                    trainNeed.CreatDate = DateTime.Now;
                    trainNeed.DeptName = deptName;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            string formNo = Request.QueryString["formNo"];
            if (string.IsNullOrEmpty(formNo))
            {
                EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
                trainNeed = new TrainNeed();
                string maxFormNo = ctx.TrainNeeds.Max(r => r.FormNo);
                formNo = "Form_Tnes_000001";
                if (!string.IsNullOrEmpty(maxFormNo))
                {
                    formNo = "Form_Tnes_" + (int.Parse(maxFormNo.Substring(10)) + 1000001).ToString().Substring(1);
                }
                trainNeed.FormNo = formNo;
                trainNeed.CreatDate = DateTime.Now;
                trainNeed.EmpId = ebi.Id;
                string deptName = ctx.Depts.Single(r => r.Id == ebi.EmpDepPos.OrderByDescending(r2 => r2.PosId).First().DeptId).Text;
                trainNeed.DeptName = deptName;
                trainNeed.NeedsYear = 2012;
                trainNeed.StatusId = 1;
                ctx.TrainNeeds.InsertOnSubmit(trainNeed);
            }
            else
            {
                trainNeed = ctx.TrainNeeds.Single(r => r.FormNo == formNo);
                trainNeed.UpdateDate = DateTime.Now;
            }
            trainNeed.WorkItems = txtWorkItems.Value;
            trainNeed.WorkTargets = txtWorkTargets.Value;
            trainNeed.FocusIssue = txtFocusIssue.Value;
            trainNeed.TrainItems = txtTrainItems.Value;
            trainNeed.TrainBasic = txtTrainBasic.Value;
            trainNeed.TrainExpert = txtTrainExpert.Value;
            trainNeed.TrainManagement = txtTrainManagement.Value;
            trainNeed.TrainAhead = txtTrainAhead.Value;
            trainNeed.TrainSuggestion = txtTrainSuggestion.Value;
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='Succeed';window.close();</script>");
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
        }
    }
}